#pragma once
#include <string>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <atomic>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <vector>

namespace ns_util
{
    const std::string path = "./tmp/";

    class TimeUtil
    {
    public:
        static std::string GetTime()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);

            return std::to_string(_time.tv_sec);
        }

        static std::string GetTimeMS()
        {
            struct timeval ms;
            gettimeofday(&ms, nullptr);

            return std::to_string(ms.tv_sec * 1000 + ms.tv_usec / 1000);
        }
    };

    class PathUtil
    {
    private:
        static std::string AddSuffix(const std::string& file_name, const std::string& suffix)
        {
            std::string path_name = path;
            path_name += file_name;
            path_name += suffix;

            return path_name;
        }   
    public:
        //编译时
        static std::string SrcFile(const std::string& file_name)
        {
            return AddSuffix(file_name, ".cpp");
        }
        
        static std::string ExeFile(const std::string& file_name)
        {
            return AddSuffix(file_name, ".exe");
        }

        static std::string CompileErrorFile(const std::string& file_name)
        {
            return AddSuffix(file_name, ".compile_err");
        }

        //运行时
        static std::string StderrFile(const std::string& file_name)
        {
            return AddSuffix(file_name, ".stderr");
        }

        static std::string StdinFile(const std::string& file_name)
        {
            return AddSuffix(file_name, ".stdin");
        }

        static std::string StdoutFile(const std::string& file_name)
        {
            return AddSuffix(file_name, ".stdout");
        }
    };

    class FileUtil
    {
    public:
        static bool isFileExist(const std::string& file_path)
        {
            struct stat st;
            int ret = stat(file_path.c_str(), &st);
            if(ret == 0)
            {
                return true;
            }
            return false;
        }

        static std::string UniqueFileName()
        {
            std::string ms = TimeUtil::GetTimeMS();
            std::atomic_uint id(0);
            id++;

            return ms + "_" + std::to_string(id);
        }

        static bool WriteFile(const std::string& file_path, const std::string& content)
        {
            std::ofstream ofs(file_path);
            if(!ofs.is_open())
            {
                return false;
            }
            ofs.write(content.c_str(), content.length());
            ofs.close();
            return true;
        }

        static bool ReadFile(const std::string& file_path, std::string* content, bool keep = false)
        {
            std::ifstream ifs(file_path);
            if(!ifs.is_open())
            {
                return false;
            }
            std::string line;
            while(std::getline(ifs, line))
            {
                *content += line;
                *content += (keep ? "\n" : "");
            }
            ifs.close();
        }
    };

    class StringUtil
    {
    public:
        static void SplitString(const std::string& line, std::vector<std::string>* content, const std::string& sep)
        {
            boost::split(*content, line, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };
}